package com.goodfather.textbook.youdao.pad.weight;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.goodfather.textbook.youdao.pad.R;
import com.goodfather.textbook.youdao.pad.pac.Constants;
import com.goodfather.textbook.youdao.pad.util.C;

//import com.umeng.analytics.MobclickAgent;

/**
 * activity 基类
 * 该类可自由定义标题栏
 */
public abstract class ToolbarActivity extends AppCompatActivity {
    public ToolbarActivity mContext;
    public FrameLayout mContainer;
    private AlertDialog dialog;
    private long lastClick;
    private WebRequestProgress mProgress;
    //    private Unbinder mUnbinder;
    public Intent mIntent;
    private View fake_status_bar;
//    private TitleBar mTitleBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ARouter.getInstance().inject(this);
        createStuff();
    }

    private void createStuff() {
        mContext = this;
        parseIntent();
        setContentView(getContentView());
//        mUnbinder = ButterKnife.bind(this);
        initViews();
        initData();
    }

    private View getContentView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View root = inflater.inflate(R.layout.activity_with_toolbar, null);
//        mTitleBar = root.findViewById(R.id.toolbar);
//        mTitleBar.setVisibility(GONE); //缺省没有title
        mContainer = (FrameLayout) root.findViewById(R.id.fl_container);
        fake_status_bar = root.findViewById(R.id.fake_status_bar);
        if (getLayoutId() > 0) {
            inflater.inflate(getLayoutId(), mContainer);
        }
        return root;
    }

    //设置布局
    protected abstract int getLayoutId();

    //解析收到的intent
    protected void parseIntent() {
        mIntent = getIntent();
    }

    //设置adapter等，应该分别设置各个view
    protected abstract void initViews();

    //设置预置数据
    protected abstract void initData();


    /**
     * 判断是否快速点击
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public boolean isFastClick() {
        long now = System.currentTimeMillis();
        if (now - lastClick >= 1500) {
            lastClick = now;
            return false;
        }
        return true;
    }

    @Override
    public Resources getResources() {
        //防止大号字体破坏布局
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    /**
     * 显示进度条
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new WebRequestProgress(this);
            mProgress.setParent(mContainer);
        }
        mProgress.showProgress();
    }

    /**
     * 显示进度条
     */
    public void showProgress(String loading) {
        if (mProgress == null) {
            mProgress = new WebRequestProgress(this);
            mProgress.setParent(mContainer);
        }
        mProgress.showProgress(loading);
    }

    /**
     * 隐藏进度条
     */
    public void dismissProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
        }
    }

    /**
     * 隐藏进度条
     */
    public void dismissProgressAll() {
        if (mProgress != null) {
            mProgress.dismissAll();
        }
    }


    @Override
    public void onBackPressed() {
        if (mProgress != null) {
            mProgress.remove();
        }
        C.removePreference(Constants.LINK_MODE);
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mUnbinder.unbind();
    }

    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
    }
}
